//
//  QZCancleVC.m
//  map
//
//  Created by 000 on 16/10/10.
//  Copyright © 2016年 faner. All rights reserved.
//

#import "QZCancleVC.h"
#import "QZCancleTableViewCell.h"
#import "QZRename.h"
#import "QZDetailVC.h"
#import "QZAnnotation.h"

//因保存引入
#import <BmobSDK/BmobObject.h>
#import "BmobSDK/Bmob.h"
//#import "QZWGS84TOGCJ02.h"
//#import "constant.h"

@interface QZCancleVC ()<UITableViewDelegate,UITableViewDataSource,QZRenameDelegate>
{
    NSMutableArray *startAllObId;
    NSMutableArray *DetarilAllObjectId;
    NSMutableArray *breakPointObjId;
    
    NSMutableArray *startApinmArray;
    NSMutableArray *lineName;
    NSMutableArray *breakPmArray;
    NSString *message;
    
//    int startCellSections;
//    int endCellSections;
    
    int breakIndex;
    //
    //    NSString *startObId;//存取出的id
    //    NSString *detailObId;//取出的详细任务的id
    //    NSMutableArray *lineDeskArray;
}
@property (nonatomic, strong)QZRename *renameView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
- (IBAction)backBC:(id)sender;
@end

@implementation QZCancleVC

- (QZRename *)renameView
{
    if (_renameView == nil) {
        _renameView = [QZRename renameViewWithFrame:self.view.bounds];
        _renameView.delegate = self;
    }
    return _renameView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addStartApinData];
    [self addBreakPointData];
    [self addDetailApinData];
    [self addLongPressGr];
    
    
}
- (void)addLongPressGr
{
    UILongPressGestureRecognizer *longPressGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    longPressGr.minimumPressDuration = 0.5f;
    longPressGr.numberOfTouchesRequired = 1;
    [self.tableView addGestureRecognizer:longPressGr];
}
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
    if (longPress.state == UIGestureRecognizerStateBegan) {
        CGPoint point = [longPress locationInView:self.tableView];
        
        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point]; // 可以获取我们在哪个cell上长按
        
        if (indexPath != nil) {
            breakIndex = (int)indexPath.row;
            [self.renameView show];
        }
        
    }
}
- (IBAction)backBC:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)addStartApinData
{
    BmobQuery *query = [BmobQuery queryWithClassName:@"StartAnnotations"];
    [query orderByAscending:@"createdAt"];
    [query calcInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        startAllObId = [NSMutableArray array];
        NSString *startObId;
        NSString *lineNameStr;
        lineName = [NSMutableArray array];
        for (BmobObject *obj in array) {
            startObId = [obj objectForKey:@"objectId"];
            
            lineNameStr = [obj objectForKey:@"lineName"];
            
            [lineName addObject:lineNameStr];
            
            [startAllObId addObject:startObId];
        }
//        startCellSections = (int)startAllObId.count;
//        [self cancleTitleName];
        [self performSelectorOnMainThread:@selector(UpdateDta) withObject:nil waitUntilDone:YES];
    }];
}
- (void)addDetailApinData
{
    BmobQuery *query = [BmobQuery queryWithClassName:@"annotations"];
    [query orderByAscending:@"createdAt"];
    [query calcInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        DetarilAllObjectId = [NSMutableArray array];
        NSString *detailObId;
        
        for (BmobObject *obj in array) {
            detailObId = [obj objectForKey:@"objectId"];
            
            [DetarilAllObjectId addObject:detailObId];
            
        }
    }];
    
}
- (void)addBreakPointData
{
    BmobQuery *query = [BmobQuery queryWithClassName:@"breakPoints"];
    [query orderByAscending:@"createdAt"];
    [query calcInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        breakPmArray = [NSMutableArray array];
        breakPointObjId = [NSMutableArray array];
        NSString *breakPId;
        for (BmobObject *obj in array) {
            NSString *breakPoints = [obj objectForKey:@"breakPointCount"];
            breakPId = [obj objectForKey:@"objectId"];
            [breakPmArray addObject:breakPoints];
            [breakPointObjId addObject:breakPId];
        }
    }];

}

#pragma mark  -------- 下载  刷新  tableView
- (void)UpdateDta{
    
//    [hud removeFromSuperview];
    //    if (arrayList.count == 0) {
    //        [self showMBHUDWithMessage:@"亲！你的收藏空空如也..."];
    //    }
    
    if (self.tableView) {
        
        [self.tableView reloadData];
    }
    
}
#pragma  mark -UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return lineName.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QZCancleTableViewCell *cell = [QZCancleTableViewCell cellWithTableView:tableView];
    
    cell.textLabel.text = lineName[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    QZDetailVC *detailVC = [[QZDetailVC alloc] init];
    detailVC.breakCount = (int)indexPath.row;
    [self presentViewController:detailVC animated:YES completion:nil];
    
}
//实现该方法 左滑动cell就会自动出现删除按钮
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    breakIndex = (int)indexPath.row;
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self deleteDetailData];
        [self deleteStartData];
        [self deleteBreakPoint];
    }
}
- (void)deleteDetailData
{
    BmobObject *bmobDetailObj;
    int indexPath = breakIndex;
    message = @"任务内容删除失败";
    int start = 0;
    int end = 0;
    for (int i = 0; i<breakPmArray.count; i++) {
        if (i< indexPath) {
            start = [breakPmArray[i] intValue]+start;
        }
        end = start + [breakPmArray[indexPath] intValue];

    }
    
    for (int i =0; i<DetarilAllObjectId.count; i++) {
        if (i>=start &&i<end) {
            bmobDetailObj = [BmobObject objectWithoutDataWithClassName:@"annotations" objectId:DetarilAllObjectId[i]];
            [bmobDetailObj deleteInBackgroundWithBlock:^(BOOL isSuccessful, NSError *error) {
                if (isSuccessful) {
                    //                删除成功 就删除 cell
                    [self performSelectorOnMainThread:@selector(upData) withObject:nil waitUntilDone:YES];
                } else if (error){
                    [self alertViewShow];
                }
            }];

        }
    }
}
- (void)deleteStartData
{
    message = @"任务开启点删除失败";
    BmobObject *bmobObject;
    bmobObject = [BmobObject objectWithoutDataWithClassName:@"StartAnnotations" objectId:startAllObId[breakIndex]];
    
    [bmobObject deleteInBackgroundWithBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
            //                删除成功 就删除 cell
            [self performSelectorOnMainThread:@selector(upData) withObject:nil waitUntilDone:YES];
        } else if (error){
            [self alertViewShow];
        } else {
            
        }
    }];
    

}
- (void)deleteBreakPoint
{
    message = @"断点删除失败";
    BmobObject *bmobObject;
    bmobObject = [BmobObject objectWithoutDataWithClassName:@"breakPoints" objectId:breakPointObjId[breakIndex]];
    
    [bmobObject deleteInBackgroundWithBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
            //                删除成功 就删除 cell
            [self performSelectorOnMainThread:@selector(upData) withObject:nil waitUntilDone:YES];
        } else if (error){
            [self alertViewShow];
        } else {
            
        }
    }];

}
#pragma mark-删除后返回主线程
- (void)upData
{
    [self addStartApinData];
    
}
- (void)alertViewShow
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Warning" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alertView show];
}

- (void)rename:(QZRename *)rename didDeterMineBtnClickWithText:(NSString *)text
{
    //此处是更新操作

    BmobObjectsBatch    *batch = [[BmobObjectsBatch alloc] init] ;

    [batch updateBmobObjectWithClassName:@"StartAnnotations" objectId:startAllObId[breakIndex] parameters:@{@"lineName": text}];
    [batch batchObjectsInBackground:^(NSArray *results, NSError *error) {
        if (error) {

        } else {
            [self upData];
        }
    }];
    
}

@end
